
board = [["5","3",".",".","7",".",".",".","."],
         ["6",".",".","1","9","5",".",".","."],
         [".","9","8",".",".",".",".","6","."],
         ["8",".",".",".","6",".",".",".","3"],
         ["4",".",".","8",".","3",".",".","1"],
         ["7",".",".",".","2",".",".",".","6"],
         [".","6",".",".",".",".","2","8","."],
         [".",".",".","4","1","9",".",".","5"],
         [".",".",".",".","8",".",".","7","9"]]

def isValidSudoku(board):
    # 暴力解法：检查三遍
    # 1.第一遍检查行
    # 2.第二遍检查列
    # 3.第三遍检查小方块
    # 可以合并到一起

    # 写一个判断一个list有没有重复元素的函数 list.count

    # 检查行
    for i in range(9):
        if cheak_repeat(board[i]) == False:
            return False

    # 检查列
    for i in range(9):
        column_board = [board[j][i] for j in range(9)]
        if cheak_repeat(column_board) == False:
            return False

    # 检查九宫格 -- 主要是把一个小块展开到一个list中
    diag_board = []

    for height in range(0, 9, 3):
        for width in range(0, 9, 3):
            print('check {}-{} block'.format(height,width))
            # 扣一个子块
            for i in range(3):
                for j in range(3):
                    diag_board.append(board[i + height][j + width])

            print(diag_board)
            if cheak_repeat(diag_board) == False:
                return False
            diag_board = []

    # 如果前面的检查都没有问题
    return True


def cheak_repeat(nums):
    for i in range(len(nums)):
        if nums[i] != '.' and nums.count(nums[i]) > 1:
            return False

    return True

if __name__ == '__main__':
    print(isValidSudoku(board))